package com.qc.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qc.common.Session;
import com.qc.entity.UserEntity;
import com.qc.service.UserService;
import com.qc.service.UserWalletService;

import net.sf.json.JSONObject;

@Controller
public class UserWalletController extends BaseController {
	
	@Resource
	private UserWalletService userWalletService;
	
	@Resource
	private UserService userService;
	
	/**
	 * 修改支付密码
	 */
	@RequestMapping(value = "/MobileUpdateWalletPwd", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileUpdateWalletPwd(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class);
		entity.setUserId(userId);
		long state = userWalletService.updateWalletPwd(entity.getWalletPwd(), userId);

		if (state > 0) {
			return returnSuccessResult("设置成功");
		}
		return returnFailedResult("设置失败");
	}
	
	/**
	 * 验证旧支付密码是否正确的接口
	 */
	@RequestMapping(value = "/MobileWalletPwdValid", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileWalletPwdValid(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class);
		entity = userWalletService.getUserInfoByWalletPwdAndUserId(entity.getWalletPwd(), userId);

		return returnSuccessResult(entity);
	}
	
	/**
	 * 判断手机号验证码是否正确
	 */
	@RequestMapping(value = "/MobileMobileCodeValid", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileMobileCodeValid(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class);
		
		String code = userService.getMobileCode(entity.getMobile());

		if (code != null && !code.equals(entity.getCode())) {
			return returnFailedResult("验证码错误");
		}
		
		return returnSuccessResult("");
	}
}
